<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/16
 * Time: 10:51
 */

namespace app\common\validate;


use think\Validate;

class Inquiry extends Validate
{
    protected $rule = [ // 定义验证规则
        'id|ID' => 'require|number',
        'vehicle_frame_no|车架号(VIN)' => 'require',
        'brand_name|品牌名称' => 'require',
        'car_model|车型名称' => 'require',
        'bus_from_company_id|询价来源' => 'require',
        'bus_to_company_ids|询价单位' => 'require',
        'vehicle_license_num|车牌号' => 'require|unique:BusInquiry',
        // 'vehicle_license_num|车牌号' => 'checkPlateNumber'
    ];

    protected $message = [ // 定义验证提示
        'vehicle_license_num.checkPlateNumber' => '车牌号格式不正确',
    ];

    protected $scene = [ // 定义验证场景
        'add' => ['vehicle_frame_no','brand_name','car_model','bus_from_company_id','bus_to_company_ids','vehicle_license_num'],
        'update' => ['id','vehicle_frame_no','brand_name','car_model','bus_from_company_id','bus_to_company_ids','vehicle_license_num.unique'=>'BusInquiry,id^vehicle_license_num']
    ];

    protected function checkPlateNumber($value, $rule, $data = [])
    {
        // 如果字段为空，则跳过验证
        if (empty($value)) {
            return true;
        }
        // 定义车牌号正则表达式
        $regex = '/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[A-Z0-9]{5}$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[A-Z0-9]{4}[DF]$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}使[0-9]{4}$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}领[0-9]{4}$/u';
        // 验证车牌号格式
        if (!preg_match($regex, $value)) {
            return false;
        }
        return true;
    }

}